<script setup lang="ts">
// App.vue 作为根组件，只需要渲染路由
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const isRouteChanging = ref(false)
const isFirstLoad = ref(true)

// 监听路由变化
router.beforeEach((to, from, next) => {
  // 第一次加载时不显示加载状态
  if (!isFirstLoad.value) {
    isRouteChanging.value = true
  }
  next()
})

router.afterEach(() => {
  // 标记第一次加载已完成
  if (isFirstLoad.value) {
    isFirstLoad.value = false
    return
  }
  
  // 延迟一小段时间再隐藏加载状态，确保页面渲染完成
  setTimeout(() => {
    isRouteChanging.value = false
  }, 100)
})
</script>

<template>
  <div class="app-container">
    <!-- 初始背景防止黑屏 -->
    <div class="initial-background"></div>
    <!-- 路由切换时的加载状态 -->
    <div v-if="isRouteChanging && !isFirstLoad" class="route-loading show">
      <div class="loading-spinner"></div>
    </div>
    <router-view v-show="!isRouteChanging || isFirstLoad" />
  </div>
</template>

<style>
/* 添加初始背景样式 */
.initial-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #ffffff;
  z-index: -3;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.route-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.route-loading.show {
  opacity: 1;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(102, 126, 234, 0.3);
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  /* 移除overflow: hidden，允许页面滚动 */
  width: 100vw;
  height: 100vh;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100vw;
  height: 100vh;
  /* 移除overflow: hidden，允许页面滚动 */
}

#app {
  width: 100vw;
  /* 移除固定高度，允许内容自然展开 */
  min-height: 100vh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: top;
}

/* 添加路由过渡动画 */
.app-container {
  transition: opacity 0.3s ease;
}

</style>